﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using FinMkts.CFD.Data;

namespace FinMkts.CFD.Rest
{
    // Start the service and browse to http://<machine_name>:<port>/CFD/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [ServiceContract]
    [XmlSerializerFormat]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class InstrumentService
    {
        // TODO: Implement the collection resource that will contain the SampleItem instances

        [Description("Get a summary list of all known market places")]
        [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Xml)]
        public List<InstrumentDC> GetAllInstruments()
        {
            using (var context = new CfdModelContainer())
            {
                context.MetadataWorkspace.LoadFromAssembly(typeof(FinancialInstrument).Assembly);
                context.MetadataWorkspace.LoadFromAssembly(typeof(ContractForDifference).Assembly);
                context.MetadataWorkspace.LoadFromAssembly(typeof(CommonStock).Assembly);
                var instruments = context.FinancialInstruments.ToList()
                    .Select(i => new InstrumentDC { Id = i.Id, Name = i.Name, CurrencyCode = i.CurrencyCode, Type = i.TypeName });
                return instruments.ToList();
            }
        }


    }
}
